/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lxx.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.lxx.dao.UserDao;
import com.lxx.entity.Page;
import com.lxx.entity.User;
import com.lxx.service.ISystemService;

/**
 * 系统管理，安全相关实体的管理类,包括用户、角色、菜单.
 * @author lxx
 * @version 2017-11-29
 */
@Service
@Transactional(readOnly = true)
public class SystemService implements InitializingBean, ISystemService {
	
	public static final int SALT_SIZE = 8;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
    RedisTemplate<String, Object> redisTemplate;

//    @Resource(name = "redisTemplate")
//    ValueOperations<Object, Object> valOpsObj;
	//@Autowired
	//private RoleDao roleDao;
	//@Autowired
	//private MenuDao menuDao;
	/*@Autowired
	private SessionDAO sessionDao;*/
	
	/*public SessionDAO getSessionDao() {
		return sessionDao;
	}*/

//	@Autowired
	//private IdentityService identityService;

	//-- User Service --//
	
	/**
	 * 获取用户
	 * @param id
	 * @return
	 */
	public User getUser(String id) {
		return userDao.get(id);
	}

	/**
	 * 根据登录名获取用户
	 * @param loginName
	 * @return
	 */
	public User getUserByLoginName(String loginName) {
		User user = new User();
		user.setLoginName(loginName);
		return userDao.getByLoginName(user);
	}
	
	public Page<User> findUser(Page<User> page, User user) {
		// 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
		//user.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
		// 设置分页参数
		//user.setPage(page);
		// 执行分页查询
		/*ListOperations<String, Object> opsForList = redisTemplate.opsForList();
		ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
		String object = (String) opsForValue.get("userList_jsonString");
		List<User> parse = (List<User>) JSONObject.parseArray(object, User.class);
		if(StringUtils.isNotBlank(object)){
			System.out.println("String有缓存");
		}*/
		@SuppressWarnings("unchecked")
		List<User> findUserList = /*(List<User>) opsForList.rightPopAndLeftPush("user_List", "user_List");
		if(!CollectionUtils.isEmpty(findUserList)){
			System.out.println("有缓存");
			page.setList(findUserList);
			return page;
		}
		findUserList =*/ userDao.findUserList(user);
		/*if(!CollectionUtils.isEmpty(findUserList)){
			//String jsonString = JSONObject.toJSONString(findUserList);
			System.out.println("无缓存");
			//opsForValue.set("userList_jsonString", jsonString);
			opsForList.rightPush("user_List", findUserList);
		}*/
		page.setList(findUserList);
		return page;
	}
	
	/**
	 * 无分页查询人员列表
	 * @param user
	 * @return
	 */
	public List<User> findUser(User user){
		// 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
		//user.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
		List<User> list = userDao.findUserList(user);
		return list;
	}

	/**
	 * 通过部门ID获取用户列表，仅返回用户id和name（树查询用户时用）
	 * @param user
	 * @return
	 */
//	@SuppressWarnings("unchecked")
//	public List<User> findUserByOfficeId(String officeId) {
//		List<User> list = (List<User>)CacheUtils.get(UserUtils.USER_CACHE, UserUtils.USER_CACHE_LIST_BY_OFFICE_ID_ + officeId);
//		if (list == null){
//			User user = new User();
//			user.setOffice(new Office(officeId));
//			list = userDao.findUserByOfficeId(user);
//			CacheUtils.put(UserUtils.USER_CACHE, UserUtils.USER_CACHE_LIST_BY_OFFICE_ID_ + officeId, list);
//		}
//		return list;
//	}
	
	@Transactional(readOnly = false)
	public void updatePasswordById(String id, String loginName, String newPassword) {
		User user = new User(id);
		user.setPassword("123456");
		userDao.updatePasswordById(user);
		// 清除用户缓存
		user.setLoginName(loginName);
		//UserUtils.clearCache(user);
//		// 清除权限缓存
//		systemRealm.clearAllCachedAuthorizationInfo();
	}
	
	@Transactional(readOnly = false)
	public void updateUserLoginInfo(User user) {
		// 保存上次登录信息
		user.setOldLoginIp(user.getLoginIp());
		user.setOldLoginDate(user.getLoginDate());
		// 更新本次登录信息
		//user.setLoginIp(UserUtils.getSession().getHost());
		user.setLoginDate(new Date());
		userDao.updateLoginInfo(user);
	}

	@Override
	public User getByLoginName(User user) {
		return null;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	///////////////// Synchronized to the Activiti end //////////////////
	
}
